/**
   Copyright 2011 Attila Fekete

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package org.afekete.filecompareprocessor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;

import org.afekete.filemetadatacreator.generated.Metadatacollection;
import org.afekete.filemetadatacreator.generated.Metadatacollection.Metadata;

public class XMLImporter {

	/**
	 * Reads an XML file with meta data of files and returns the meta data in a List object.
	 * 
	 * @param exportFile input filename
	 * @return list of meta data of files
	 * @throws JAXBException if something went wrong during the XML generation
	 * @throws IOException if something went wrong in file handling
	 */
	
	public static List<Metadata> read(File xmlFile) throws JAXBException, XMLStreamException, IOException {
		
        JAXBContext jc = JAXBContext.newInstance("org.afekete.filemetadatacreator.generated");
		Unmarshaller unmarshaller = jc.createUnmarshaller();
		
		FileInputStream fis = new FileInputStream(xmlFile);
		Metadatacollection metadatacollection = (Metadatacollection) unmarshaller.unmarshal(fis);
       
		fis.close();
		
		return metadatacollection.getMetadata();
		
	}
	
	/**
	 * Reads an XML file with meta data of files and returns the versionpath attribute's value 
	 * 
	 * @param xmlFile input fileName
	 * @return versionpath value
	 * @throws JAXBException
	 * @throws XMLStreamException
	 * @throws IOException
	 */
	public static String readVersionPath(File xmlFile) throws JAXBException, XMLStreamException, IOException {
		
		JAXBContext jc = JAXBContext.newInstance("org.afekete.filemetadatacreator.generated");
		Unmarshaller unmarshaller = jc.createUnmarshaller();
		
		FileInputStream fis = new FileInputStream(xmlFile);
		Metadatacollection metadatacollection = (Metadatacollection) unmarshaller.unmarshal(fis);
		
		fis.close();
		
		return metadatacollection.getVersionpath();
        
	}
}
